//
//  RectangleTool.m
//  iPadDev01
//
//  Created by smu on 11-7-29.
//  Copyright 2011年 duk. All rights reserved.
//

#import "RectangleTool.h"
#import "PathDrawingInfo.h"
#import "SynthesizeSingleton.h"

@implementation RectangleTool
@synthesize delegate;
SYNTHESIZE_SINGLETON_FOR_CLASS(RectangleTool);
- init 
{
    if ((self = [super init])) {
        trackingTouches = [[NSMutableArray array] retain];
        startPoints = [[NSMutableArray array] retain];
    }
    return self;
}

- (void)activate{}

- (void)deactivate
{
    [trackingTouches removeAllObjects];
    [startPoints removeAllObjects];
}
//矩形工具能够跟踪多个同时出现的触摸点，最终为每一个触摸点创建一条新直线
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView *touchedView = [delegate viewForUseWithTool:self];
    for (UITouch *touch in [event allTouches]) {
        //remember the touch, and its original start point, for future
        [trackingTouches addObject:touch];
        CGPoint location = [touch locationInView:touchedView];
        [startPoints addObject:[NSValue valueWithCGPoint:location]];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView *touchedView = [delegate viewForUseWithTool:self];
    for (UITouch *touch in [event allTouches]) {
        //make an ellipse/oval from the start point to the current point
        NSUInteger touchIndex = [trackingTouches indexOfObject:touch];
        //only if we actually remember the start of this touch ...
        if (touchIndex != NSNotFound) {
            CGPoint startPoint = [[startPoints objectAtIndex:touchIndex] CGPointValue];
            CGPoint endPoint = [touch locationInView:touchedView];
            CGRect rect = CGRectMake(startPoint.x, startPoint.y, endPoint.x-startPoint.x, endPoint.y - startPoint.y);
            UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
            PathDrawingInfo *info = [PathDrawingInfo pathDrawingInfoWithPath:path fillColor:delegate.fillColor strokeColor:delegate.strokeColor];
            [delegate addDrawable:info];
            [trackingTouches removeObjectAtIndex:touchIndex];
            [startPoints removeObjectAtIndex:touchIndex];
        }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

- (void)drawTemporary
{
    UIView *touchedView = [delegate viewForUseWithTool:self];
    for (int i = 0; i < [trackingTouches count]; i++) {
        UITouch *touch = [trackingTouches objectAtIndex:i];
        CGPoint startPoint = [[startPoints objectAtIndex:i] CGPointValue];
        CGPoint endPoint = [touch locationInView:touchedView];
        CGRect rect = CGRectMake(startPoint.x, startPoint.y, endPoint.x-startPoint.x, endPoint.y - startPoint.y);
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
        [delegate.fillColor setFill];
        [path fill];
        [delegate.strokeColor setStroke];
        [path stroke];
    }
}

- (void)dealloc
{
    [trackingTouches release];
    [startPoints release];
    self.delegate = nil;
    [super dealloc];
}

@end
